<?php

function smarty_modifier_var($value, $type, $config='') {
	global $db, $site;
	if ($type == 'ImageTag') {
		$image = $db->fetchRow("SELECT id, filename, caption, description, link FROM cms_images WHERE id=? AND site_id=?", array($value, $site['id']));
		$value = '';
		if ($image['link']) $value .= '<a href="' . $image['link'] . '">';
		$value .= '<img src="/images/' . ($config ? $config . '/' : '') . $image['id'] . '/'. $image['filename'] . '" alt="' . htmlentities($image['caption'], ENT_COMPAT, 'UTF-8') . '" title="' . htmlentities($image['description'], ENT_COMPAT, 'UTF-8') . '" />';
		if ($image['link']) $value .= '</a>';
	}
	elseif ($type == 'Image') {
		$image = $db->fetchRow("SELECT id, filename, caption, description, link FROM cms_images WHERE id=? AND site_id=?", array($value, $site['id']));
		return $image;
	}
	elseif ($type == 'ImageFolder') {
		$sort = in_array($config, array('filename', 'created', 'modified', 'rand()', 'sort')) ? $config : 'sort';
		$value = $db->fetchAll("SELECT id, filename, caption, description, link FROM cms_images WHERE folder_id=? AND site_id=? ORDER BY {$sort}", array($value, $site['id']));
	}
	elseif ($type == 'File') {
		$file = $db->fetchRow("SELECT id, filename, caption, description FROM cms_files WHERE id=? AND site_id=?", array($value, $site['id']));
		$value = "/files/{$file['id']}/{$file['filename']}";
	}
	elseif ($type == 'FileFolder') {
		$sort = in_array($config, array('filename', 'created', 'modified')) ? $config : 'id';
		$value = $db->fetchAll("SELECT id, filename, caption, description FROM cms_files WHERE folder_id=? AND site_id=? ORDER BY ?", array($value, $site['id'], $sort));
		for($i=0;$i<count($value);$i++) {
			$value[$i]['path'] = "/files/{$value[$i]['id']}/{$value[$i]['filename']}";
		}
	}
	elseif ($type == 'RichText') {
		// add anchors to all the header tags
		$value = preg_replace("/<h(\d)>([^<]+)</e", '"<a name=\"" . make_path(html_entity_decode("$2")) . "\"></a><h$1>" . stripslashes("$2") . "<";', $value);
		
		// if called with 'headers' in config, then parse into array and return the headers for contents display
		if ($config == 'headers') {
			$headers = array();
			$parent  = &$headers;
			$parents = array();
			$last_level = null;
			
			preg_match_all('/<a name="(.*?)"><\/a><h([1-6])>(.*?)<\/h[1-6]>/', $value, $matches);
			for($i=0;$i<sizeof($matches[1]);$i++) {
				$name  = $matches[1][$i];
				$level = $matches[2][$i];
				$title = $matches[3][$i];
				if (!$last_level) $last_level = $level;
				
				if ($level > $last_level) {
					$parents[] = &$parent;
					$parent = &$parent[count($parent)-1]['children'];
					$last_level = $level;
				}
				if ($level < $last_level) {
					for($j=$last_level;$j>$level;$j--) {
						$parent = &$parents[count($parents)-1];
						array_pop($parents);
					}
					$last_level = $level;
				}
				array_push($parent, array('name' => $name, 'level' => $level, 'title' => $title, 'children' => array()));
			}
			$value = $headers;
		}
	}

	return $value;
}
$smarty->register_modifier('var', 'smarty_modifier_var');
